import matplotlib
import matplotlib.pyplot as plt
import numpy as np

matplotlib.rcParams['font.family'] = 'Microsoft YaHei'  # Windows 常用

# 设置点的坐标
x0, y0 = 2, 3

# 创建图形和坐标轴
fig, ax = plt.subplots(figsize=(8, 6))

# 设置坐标轴范围
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)

# 绘制网格线
ax.grid(True, linestyle='--', alpha=0.7)

# 绘制坐标轴
ax.axhline(y=0, color='k', linestyle='-', linewidth=1)
ax.axvline(x=0, color='k', linestyle='-', linewidth=1)

# 标记坐标轴箭头
ax.arrow(0, 0, 10, 0, head_width=0.5, head_length=1, fc='k', ec='k')
ax.arrow(0, 0, 0, 10, head_width=0.5, head_length=1, fc='k', ec='k')

# 添加坐标轴标签
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_title('二维坐标系中的点与直线')

# 绘制点 (x0, y0)
ax.plot(x0, y0, 'ro', markersize=10)

# 绘制穿过该点的任意直线
# 方法一：使用斜率k和截距b
k = 1.5  # 任意选择的斜率
b = y0 - k * x0  # 确保直线通过点(x0, y0)

# 生成x值范围内的点
x_values = np.linspace(-10, 10, 100)
y_values = k * x_values + b

# 绘制直线
ax.plot(x_values, y_values, 'b-', linewidth=2)

# 添加图例
ax.legend()

ax.text(2.2,3,"$(x_0,y_0)$")
# 显示图形
plt.tight_layout()
plt.show()
